package com.rockcent.mall.helper;

import com.rockcent.common.utils.RDateFormatUtils;
import com.rockcent.mall.lib.domain.enumclass.MerchantOrderPathType;
import com.rockcent.mall.lib.domain.enumclass.OrderType;
import com.rockcent.mall.lib.domain.enumclass.ProductShippingType;
import com.rockcent.mall.lib.dto.MallOrderDto;
import com.rockcent.mall.lib.dto.MerchantOrderDto;
import com.rockcent.mall.lib.dto.MerchantOrderItemDto;
import jxl.CellView;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by DK on 16/12/14.
 * 平台对账明细报表导出, 数据格式比较怪异, 所以单独写一个方法导出
 */
@Component
public class MallBalanceAccountExportHelper {

    public void mallBalanceAccountExportExcel (List<MallOrderDto> mallOrderDtoList, HttpServletResponse response) throws IOException, WriteException {

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String filename = sdf.format(date);
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=" + filename);

        ServletOutputStream outputStream = response.getOutputStream();

        String columnName = "平台订单号,流水号,买家ID,实付金额,下单时间,商户名称,订单类型,订单来源,供应商名称,供应商ID,分销商名称,分销商ID,分享者ID";
        // 创建excel工作簿
        WritableWorkbook wwb = Workbook.createWorkbook(outputStream);
        WritableSheet sheet = wwb.createSheet("第一页",0);

        //设置单元格的文字格式
        WritableFont wf = new WritableFont(WritableFont.ARIAL,12, WritableFont.NO_BOLD,false,
                UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
        WritableCellFormat wcf = new WritableCellFormat(wf);
        wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
        wcf.setAlignment(Alignment.CENTRE);
        sheet.setRowView(1, 500);
        CellView cellView = new CellView();
        cellView.setAutosize(true);
        sheet.setColumnView(0, cellView);

        // 开始写入第一行(即标题栏)
        String[] columnNames = columnName.split(",");
        for (int j=0;j<columnNames.length;j++){
            sheet.addCell(new Label(j, 0, columnNames[j], wcf));
        }

        int row = 0;
        for (int i = 0; i < mallOrderDtoList.size(); i++){
            sheet.setColumnView(row, cellView);
            MallOrderDto mallOrderDto = mallOrderDtoList.get(i);
            sheet.addCell(new Label(0, row + 1, mallOrderDto.getOrderNum(), wcf));
            sheet.addCell(new Label(1, row + 1, mallOrderDto.getPaymentTransaction(), wcf));
            sheet.addCell(new Label(2, row + 1, mallOrderDto.getCustomerId()+"", wcf));
            sheet.addCell(new Label(3, row + 1, mallOrderDto.getDiscountTotalAmount().toString(), wcf));
            sheet.addCell(new Label(4, row + 1, RDateFormatUtils.format(mallOrderDto.getDateCreated()), wcf));

            List<MerchantOrderDto> merchantOrderDtoList = mallOrderDto.getMerchantOrderDtoList();
            int g = 0;
            for (int j = 0; j < merchantOrderDtoList.size(); j++) {
                sheet.setColumnView(row + g + j, cellView);
                MerchantOrderDto merchantOrderDto = merchantOrderDtoList.get(j);
                sheet.addCell(new Label(5, row + g + j + 1, merchantOrderDto.getMerchantName(), wcf));
                if (ProductShippingType.SHIPPING == ProductShippingType.valueOf(merchantOrderDto.getProductShoppingType())) {
                    sheet.addCell(new Label(6, row + g + j + 1, "物流订单", wcf));
                }
                if (ProductShippingType.SHOP_PICKUP == ProductShippingType.valueOf(merchantOrderDto.getProductShoppingType())) {
                    sheet.addCell(new Label(6, row + g + j + 1, "自提订单", wcf));
                }
                if (OrderType.SERVICE == OrderType.valueOf(merchantOrderDto.getOrderType())) {
                    sheet.addCell(new Label(6, row + g + j + 1, "服务订单", wcf));
                }
                List<MerchantOrderItemDto> merchantOrderItemDtoList = merchantOrderDto.getMerchantOrderItemDtoList();
                for (int k = 0; k < merchantOrderItemDtoList.size(); k++) {
                    sheet.setColumnView(row + g + j + k, cellView);
                    MerchantOrderItemDto merchantOrderItemDto = merchantOrderItemDtoList.get(0);
                    sheet.addCell(new Label(7, row + g + j + k + 1, MerchantOrderPathType.valueOf(merchantOrderItemDto.getPathType()).toString(), wcf));
                    sheet.addCell(new Label(8, row + g + j + k + 1, merchantOrderItemDto.getSupplierMerchantName() == null ? "" : merchantOrderItemDto.getSupplierMerchantName() + "", wcf));
                    sheet.addCell(new Label(9, row + g + j + k + 1, merchantOrderItemDto.getSupplierMerchantId() == null ? "" : merchantOrderItemDto.getSupplierMerchantId() + "", wcf));
                    sheet.addCell(new Label(10, row + g + j + k + 1, merchantOrderItemDto.getDistributorMerchantName() == null ? "" : merchantOrderItemDto.getDistributorMerchantName() + "", wcf));
                    sheet.addCell(new Label(11, row + g + j + k + 1, merchantOrderItemDto.getDistributorMerchantId() == null ? "" : merchantOrderItemDto.getDistributorMerchantId() + "", wcf));
                    sheet.addCell(new Label(12, row + g + j + k + 1, merchantOrderItemDto.getSharerId() == null ? "" : merchantOrderItemDto.getSharerId() + "", wcf));
                }
            }

            if (mallOrderDto.getMerchantOrderDtoList().size() > 0) {
                for (MerchantOrderDto orderDto : mallOrderDto.getMerchantOrderDtoList()) {
                    if (orderDto.getMerchantOrderItemDtoList().size() > 0) {
                        row = row + orderDto.getMerchantOrderItemDtoList().size();
                    }
                }
            }
            //sheet.mergeCells(0, row, 0 , row - 1); 合并单元格
            g++;
        }

        // 写入数据
        wwb.write();
        // 关闭文件
        wwb.close();
        // 关闭输出流
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }
}
